/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pigx.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pig4cloud.pigx.admin.api.entity.MechanismUserStatistics;
import com.pig4cloud.pigx.admin.api.vo.UserStatisticsVo;
import com.pig4cloud.pigx.admin.mapper.MechanismUserMapper;
import com.pig4cloud.pigx.admin.mapper.MechanismUserStatisticsMapper;
import com.pig4cloud.pigx.admin.service.MechanismUserStatisticsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 四乱整治 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-20
 */
@Service
@AllArgsConstructor
@Slf4j
public class MechanismUserStatisticsServiceImpl extends ServiceImpl<MechanismUserStatisticsMapper, MechanismUserStatistics> implements MechanismUserStatisticsService {


    private final MechanismUserStatisticsMapper mapper;


    /**
     * @param
     * @return UserStatisticsVo
     * @author lfx
     * @date 2024/8/7 15:11
     * @Desc 人员配置统计查询
     **/
    @Override
    public UserStatisticsVo queryStatistics() {
        try {
            List<MechanismUserStatistics> statisticsList = mapper.selectList(null);
            Integer sum = statisticsList.stream().reduce(0, (a, b) -> a + b.getCount(), Integer::sum);
            ArrayList<UserStatisticsVo.UserStatisticsMajor> majorRatios = Lists.newArrayList();
            statisticsList.stream().forEach((item) -> {
                majorRatios.add(UserStatisticsVo.UserStatisticsMajor.builder()
                        .ratio(String.format("%.2f", (double) item.getCount() / sum * 100) + "%")
                        .count(item.getCount())
                        .major(item.getMajor())
                        .build());
            });
            UserStatisticsVo userStatisticsVo = UserStatisticsVo.builder()
                    .majorRatioList(majorRatios)
                    .sum(sum)
                    .build();
            return userStatisticsVo;
        } catch (Exception e) {
            log.error("业务处理异常：{}", e.getMessage());
            throw new RuntimeException("业务处理异常", e);
        }
    }

    /**
     *
     * @param
     * @return List<MechanismUserStatistics>
     * @author lfx
     * @date 2024/8/12 10:08
     * @Desc 人员统计配置列表查询
     **/
    @Override
    public List<MechanismUserStatistics> queryList() {
        List<MechanismUserStatistics> mechanismUserStatistics = mapper.selectList(null);
        return mechanismUserStatistics;
    }

}
